#include <windows.h>
#include "XenMemory.h"

XenMemory::XenMemory()
{
	this->xenimusProcess = GetCurrentProcess();
}

XenMemory::XenMemory(HANDLE xenimusProcess)
{
	//DWORD xenimusPid = Utils::GetProcId("Xenimus.exe");
	this->xenimusProcess = xenimusProcess;//OpenProcess(PROCESS_ALL_ACCESS, FALSE, xenimusPid);
}

byte XenMemory::ReadMemLocByte(int address)
{
	byte memLocByte;
	ReadProcessMemory(xenimusProcess, (LPCVOID)address, &memLocByte, sizeof(byte), NULL);

	return memLocByte;
}

unsigned int XenMemory::ReadMemLocUInt(int address)
{
	unsigned int memLocInt;
	ReadProcessMemory(xenimusProcess, (LPCVOID)address, &memLocInt, sizeof(int), NULL);

	return memLocInt;
}

int XenMemory::ReadMemLocInt(int address)
{
	int memLocInt;
	ReadProcessMemory(xenimusProcess, (LPCVOID)address, &memLocInt, sizeof(int), NULL);

	return memLocInt;
}

unsigned short XenMemory::ReadMemLocUShort(int address)
{
	unsigned short memLocInt;
	ReadProcessMemory(xenimusProcess, (LPCVOID)address, &memLocInt, sizeof(short), NULL);

	return memLocInt;
}

string XenMemory::ReadMemLocString(int address, int size)
{
	char *memLocCharArr = new char[size + 1];
	memLocCharArr[size] = NULL;
	ReadProcessMemory(xenimusProcess, (LPCVOID)address, memLocCharArr, size * sizeof(char), NULL);

	string retString(memLocCharArr, size + 1);
	delete [] memLocCharArr;
	memLocCharArr = NULL;

	return retString;
}

void XenMemory::WriteMemLocInt(int address, int value)
{
	WriteProcessMemory(xenimusProcess, (LPVOID)address, (LPCVOID)&value, 4, NULL);
}

void XenMemory::WriteMemLocShort(int address, short value)
{
	WriteProcessMemory(xenimusProcess, (LPVOID)address, (LPCVOID)&value, 2, NULL);
}

short XenMemory::ReadClickCounter()
{
	return ReadMemLocUShort(0x1585e94);
}

void XenMemory::WriteClickCounter(short value)
{
	WriteMemLocShort(0x1585e94, value);
}

short XenMemory::ReadPID()
{
	return ReadMemLocUShort(ConfigFile::PID);
}

float XenMemory::ReadMPPercent()
{
	byte mpPercent = ReadMemLocByte(ConfigFile::MPPercent);

	return mpPercent / 63.0;
}

float XenMemory::ReadHPPercent()
{
	byte hpPercent = ReadMemLocByte(ConfigFile::HPPercent);

	return hpPercent / 63.0;
}

float XenMemory::ReadXPPercent()
{
	byte xpPercent = ReadMemLocByte(ConfigFile::XPPercent);

	return xpPercent / 63.0;
}

byte XenMemory::ReadLevel()
{
	return ReadMemLocByte(ConfigFile::Level);
}

byte XenMemory::ReadViewVar()
{
	return ReadMemLocByte(ConfigFile::View);
}

byte XenMemory::ReadInGameVar()
{
	return ReadMemLocByte(ConfigFile::InGame);
}

byte XenMemory::ReadPage()
{
	return ReadMemLocByte(ConfigFile::Page);
}

byte XenMemory::ReadServer()
{
	return ReadMemLocByte(ConfigFile::Server);
}

byte XenMemory::ReadInChat()
{
	return ReadMemLocByte(ConfigFile::InChat);
}

short XenMemory::ReadMaxHP()
{
	return ReadMemLocUShort(ConfigFile::MaxHP);
}

short XenMemory::ReadMaxMP()
{
	return ReadMemLocUShort(ConfigFile::MaxMP);
}

int XenMemory::ReadCurrentXP()
{
	return ReadMemLocUInt(ConfigFile::CurrentXP);
}

int XenMemory::ReadFPS()
{
	return ReadMemLocUInt(ConfigFile::FPS);
}

int XenMemory::ReadMoney()
{
	return ReadMemLocUInt(ConfigFile::Money);
}

byte XenMemory::ReadCurrentLight()
{
	return ReadMemLocByte(ConfigFile::TimeOfDay);
}

int XenMemory::ReadLocX()
{
	return ReadMemLocUInt(ConfigFile::LocationX);
}

int XenMemory::ReadLocY()
{
	return ReadMemLocUInt(ConfigFile::LocationY);
}

void XenMemory::WriteLocX(int value)
{
	WriteMemLocInt(ConfigFile::LocationX, value);
}

void XenMemory::WriteLocY(int value)
{
	WriteMemLocInt(ConfigFile::LocationY, value);
}


